package dom;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;

import javax.xml.transform.sax.SAXResult;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.SAXWriter;
import org.dom4j.io.XMLWriter;

public class Test_dom4j {

	public static void main(String[] args) {
		Test_dom4j.readXml();
		// Test_dom4j.delXml();
		// Test_dom4j.updateXml();
	//	Test_dom4j.insertXml();
	}

	private static void delXml() {

		File xmlFile = new File("./src/student.xml");
		SAXReader saxReader = new SAXReader();

		try {
			Document document = saxReader.read(xmlFile);

			Element rootElement = document.getRootElement();

			Element parenElement = (Element) rootElement.elements("root")
					.get(0);

			List<Element> list = parenElement.elements("student");

			for (Element student : list) {

				String id = student.attributeValue("id");

				if (id.equals("03")) {
					// 只能有父节点的remove方法移除
					parenElement.remove(student);
					break;
				}

			}

			// 将更改的Document节点重新写回到XML文件中
			OutputStream outputStream = new FileOutputStream(xmlFile);
			XMLWriter xmlWriter = new XMLWriter(outputStream);
			xmlWriter.write(document);
			xmlWriter.flush();
			xmlWriter.close();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private static void readXml() {

		// 创建一个xml的解析器
		File xmlFile = new File("./src/student.xml");
		SAXReader reader = new SAXReader();

		try {
			// 获取文档对象
			Document document = reader.read(xmlFile);

			// 获取根节点,注意这里的Element对象必须是org.dom4j.Element类的对象，不同于DOM方法解析xml文件
			Element rootElement = document.getRootElement();

			// 获取root节点
			Element parenElment = (Element) rootElement.elements().get(0);

			List<Element> list = parenElment.elements("student");

			for (Element student : list) {
				String stu_id = student.attributeValue("id");

				String name = student.element("name").getText();
				String age = student.elementText("name"); // 相当于student.element("age").getText();
				String sex = student.elementText("sex");
				String content = student.elementText("content");

				System.out.println("id=" + stu_id + "name=" + name + "age="
						+ age + "sex=" + sex + "content" + content);

			}

		} catch (DocumentException e) {

			e.printStackTrace();
		}
	}

	private static void updateXml() {
		File xmlFile = new File("./src/student.xml");
		SAXReader saxReader = new SAXReader();

		try {
			Document document = saxReader.read(xmlFile);

			Element rootElement = document.getRootElement();

			Element parenElement = (Element) rootElement.elements("root")
					.get(0);

			List<Element> stuList = parenElement.elements("student");

			for (Element student : stuList) {
				String id = student.attributeValue("id");

				if (id.equals("01")) {
					student.element("name").setText("修改后的01");
					student.element("age").setText("22");
					// 先把CDATA节点删除再添加
					student.remove(student.element("content"));
					student.addElement("content").addCDATA("$#^%$&*$674");

				}

			}

			// 保存修改后的数据
			OutputStream outputStream = new FileOutputStream(xmlFile);

			OutputFormat format = OutputFormat.createPrettyPrint();

			format.setIndent(true);
			format.setIndent("\t");
			format.setEncoding("UTF-8");
			format.setTrimText(true);

			XMLWriter xmlWriter = new XMLWriter(outputStream, format);
			xmlWriter.write(document);
			xmlWriter.flush();
			xmlWriter.close();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private static void insertXml() {
		File xmlFile=new File("./src/student.xml");
		
		SAXReader saxReader=new SAXReader();
		
		
		try {
			Document document=saxReader.read(xmlFile);
			
			Element rootElement=document.getRootElement();
			
			Element parenElement=rootElement.element("root");
			
			Element studentElement=parenElement.addElement("student");
			studentElement.addAttribute("id", "03");
			
			studentElement.addElement("name").setText("新增");
			studentElement.addElement("age").setText("12");
			studentElement.addElement("sex").setText("男");
			studentElement.addElement("content").addCDATA("#$%^#$%#$^");
			
			OutputStream out=new FileOutputStream(xmlFile);
			OutputFormat format=OutputFormat.createPrettyPrint();
			
			format.setIndent(true);
			format.setIndent("\t");;
			
			format.setEncoding("UTF-8");
			format.setTrimText(true);
			
			XMLWriter xmlWriter=new XMLWriter(out,format);
			xmlWriter.write(document);
			xmlWriter.flush();
			xmlWriter.close();
					
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}

}
